//
//  VerificationCodeTableViewCell.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/15.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "VerificationCodeTableViewCell.h"
#import "VerificationCodeView.h"
#import "ObtainCodeButton.h"

static NSString * phoneStr;
static NSString * imageCodeStr;
static NSString * codeStr;
//
static GLGWView * phonr_lineView;
static GLGWView * code_lineView;
static GLGWView * mcode_lineView;


@interface VerificationCodeTableViewCell (){
    NSInteger index;//定义全局变量
}
@end

@implementation VerificationCodeTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier thrRow:(NSInteger)ver_rowNum{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //
        GLGWView * lineView = [[GLGWView alloc] initWithInSuperView:self.contentView];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(kWidthProportion_In_iPhoneTriploid(30));
            make.right.equalTo(self.contentView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(30));
            make.bottom.equalTo(self.contentView).offset(0);
            make.height.equalTo(@1);
        }];
        //15
        index = 0;
        GLGWTextField * codeTextField = [[GLGWTextField alloc] initWithFrame:CGRectMake(kWidthProportion_In_iPhoneTriploid(30), kHeightProportion_In_iPhoneTriploid(10), KScreenWidth - kWidthProportion_In_iPhoneTriploid(140), kHeightProportion_In_iPhoneTriploid(52)) titlePlaceholder:@"输入手机号" theFont:SYSTEMFONT(16) theTitColor:kColor_Rule_Black inSuperView:self.contentView];
        codeTextField.tag = ver_rowNum + 160;
        codeTextField.delegate = nil;
        codeTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
        [codeTextField addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
        [codeTextField addTarget:self action:@selector(textField1TextBegin:) forControlEvents:UIControlEventEditingDidBegin];
        if (ver_rowNum == 0) {
            phonr_lineView = lineView;
            codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        }
        if (ver_rowNum == 1) {
            code_lineView = lineView;
            codeTextField.keyboardType = UIKeyboardTypeASCIICapable;
            codeTextField.placeholder = @"输入验证码";
            //图片z验证码
//            verCodeView = [[VerificationCodeView alloc] initWithFrame:CGRectMake(KScreenWidth - kWidthProportion_In_iPhoneTriploid(90), kHeightProportion_In_iPhoneTriploid(11), kWidthProportion_In_iPhoneTriploid(60), kHeightProportion_In_iPhoneTriploid(40)) andCharCount:4 andLineCount:20];
            /// 返回验证码数字
//            kWeakSelf(self);
//            verCodeView.changeValidationCodeBlock = ^(void){
//                if (weakself.delegate && [weakself.delegate respondsToSelector:@selector(changeVerificationCode)]) {
//                    [weakself.delegate changeVerificationCode];
//                }
//            };
//            [self addSubview:verCodeView];
            self.codePictureImageView = [[GLGWImageView alloc] initWithFrame:CGRectMake(KScreenWidth - kWidthProportion_In_iPhoneTriploid(90), kHeightProportion_In_iPhoneTriploid(11), kWidthProportion_In_iPhoneTriploid(60), kHeightProportion_In_iPhoneTriploid(40)) inSuperView:self.contentView];
            UITapGestureRecognizer * tap_ImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap_TopViewAction:)];
            tap_ImageView.numberOfTouchesRequired = 1;
            tap_ImageView.numberOfTapsRequired = 1;
            [self.codePictureImageView addGestureRecognizer:tap_ImageView];
            self.codePictureImageView.userInteractionEnabled = YES;
            self.codePictureImageView.backgroundColor = kRandomColor;
        }
        if (ver_rowNum == 2) {
            mcode_lineView = lineView;
            codeTextField.keyboardType = UIKeyboardTypeNumberPad;
            codeTextField.placeholder = @"输入手机验证码";
            //短信验证码
            ObtainCodeButton * obtabinBut = [[ObtainCodeButton alloc] initWithTitleLableFontType:SYSTEMFONT(12) titleColor:kColor_Rule_Yellow inSuperView:self.contentView];
            obtabinBut.title = @"  获取验证码";
            [obtabinBut addTarget:self action:@selector(veriCodeAction:) forControlEvents:UIControlEventTouchUpInside];
            [obtabinBut mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.contentView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(30));
                make.top.equalTo(self.contentView.mas_top).offset(kHeightProportion_In_iPhoneTriploid(26));
                make.height.equalTo(@(kHeightProportion_In_iPhoneTriploid(20)));
            }];
        }
        
    }
    return self;
}

-(void)tap_TopViewAction:(UITapGestureRecognizer *)tap{
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeVerificationCode)]) {
        [self.delegate changeVerificationCode];
    }
}

-(void)veriCodeAction:(GLGWButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(verificationCode:)]) {
        [self.delegate verificationCode:sender];
    }
}

-(void)textField1TextChange:(GLGWTextField *)textField{
    switch (textField.tag) {
        case 160:{
            if (textField.text.length > index) {
                if (textField.text.length == 4 || textField.text.length == 9 ) {//输入
                    NSMutableString * str = [[NSMutableString alloc ] initWithString:textField.text];
                    [str insertString:@" " atIndex:(textField.text.length-1)];
                    textField.text = str;
                }if (textField.text.length >= 13 ) {//输入完成
                    textField.text = [textField.text substringToIndex:13];
//                    [textField resignFirstResponder];
                }
                index = textField.text.length;
                
            }else if (textField.text.length < index){//删除
                if (textField.text.length == 4 || textField.text.length == 9) {
                    textField.text = [NSString stringWithFormat:@"%@",textField.text];
                    textField.text = [textField.text substringToIndex:(textField.text.length-1)];
                }
                index = textField.text.length;
            }
            phoneStr = textField.text;
        }
            break;
        case 161:{
            if (textField.text.length>4) {
                textField.text = [textField.text substringToIndex:4];
            }
            imageCodeStr= textField.text;
        }
            break;
        case 162:{
            if (textField.text.length>6) {
                textField.text = [textField.text substringToIndex:6];
            }
            codeStr = textField.text;
        }
            break;
            
        default:
            break;
    }
    //原始字符串，全部剔除空格
    NSString *originNoSpaceString = [phoneStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (self.delegate && [self.delegate respondsToSelector:@selector(phoneNumStr:picVerification:codeString:)]) {
        [self.delegate phoneNumStr:originNoSpaceString picVerification:imageCodeStr codeString:codeStr];
    }
}

-(void)textField1TextBegin:(GLGWTextField *)textField{
    switch (textField.tag) {
        case 160:{
            phonr_lineView.backgroundColor = kColor_Rule_Yellow;
            code_lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
            mcode_lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        }
            break;
        case 161:{
            phonr_lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
            code_lineView.backgroundColor = kColor_Rule_Yellow;
            mcode_lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        }
            break;
        case 162:{
            phonr_lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
            code_lineView.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
            mcode_lineView.backgroundColor = kColor_Rule_Yellow;
        }
            break;
            
        default:
            break;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
